package com.xuhuan.practice.controller;

import com.xuhuan.practice.response.Result;
import com.xuhuan.practice.service.OSSService;
import com.xuhuan.practice.utils.MinioUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author: huan.xu
 * @create: 2024/6/12 9:27
 * @description: 文件上传控制器
 **/
@RestController
@RequestMapping("/oss")
public class OSSController {

  @Autowired
  private OSSService ossService;

  /**
   * 文件上传
   *
   * @param file
   * @return
   * @throws Exception
   */
  @PostMapping("/upload")
  public Result<String> upload(MultipartFile file) {
    return Result.success(ossService.upload(file));
  }

  /**
   * 下载
   *
   * @param storagePath /20240612/154a1a61a5484a37838d29bd0dc51404.png
   * @param request
   * @param response
   */
  @GetMapping("/download")
  @CrossOrigin(originPatterns = "*", allowCredentials = "true", allowedHeaders = "*")
  public void download(@RequestParam("storagePath") String storagePath,
      HttpServletRequest request, HttpServletResponse response) {
    ossService.download(storagePath, request, response);
  }
}
